﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Function;

namespace UI.UserControl.UI
{

    /// <summary>
    /// ajax选择器 使用此选择器的服务端处理文件必须返回如下json格式[{entityid:1,entityvalue:'sample'}]
    /// </summary>
    public partial class Selector : System.Web.UI.UserControl
    {
        #region 私有属性
        /// <summary>
        /// 最大显示项的数量
        /// </summary>
        private int _MaxItemCount = 10;
        /// <summary>
        /// 搜索文本框宽度(像素)
        /// </summary>
        private int _TextBoxWidth = 200;

        /// <summary>
        /// 未选择的提示
        /// </summary>
        private string _EmptyTip = "请选择一个项";

        #endregion
        #region 属性

        /// <summary>
        /// 未选择的提示
        /// </summary>
        public string EmptyTip { get { return _EmptyTip; } set { _EmptyTip = value; } }

        /// <summary>
        /// 自定义查询数据项
        /// </summary>
        public string CustomizeQuery { get; set; }

        /// <summary>
        /// 失去焦点时自动隐藏搜索框
        /// 警告:若要使用SelectedEntityID属性(选中项)则该属性
        /// </summary>
        public bool AutoHideWhenBlur { get; set; }

        /// <summary>
        /// 最大显示项的数量
        /// </summary>
        public int MaxItemCount { get { return _MaxItemCount; } set { _MaxItemCount = value; } }

        /// <summary>
        /// 搜索文本框宽度(像素)
        /// </summary>
        public int TextBoxWidth { get { return _TextBoxWidth; } set { _TextBoxWidth = value; } }

        /// <summary>
        /// 搜素文本框水印文本
        /// </summary>
        public string TextBoxWatermarkText { get; set; }

        /// <summary>
        /// 文本框默认文本
        /// </summary>
        public string DefaultValue { get; set; }

        /// <summary>
        /// ajax请求文件路径 上级文件会自动获取目录深度
        /// </summary>
        public string ServiceFilePath { get; set; }

        /// <summary>
        /// 选中的实体id值
        /// </summary>
        public string SelectedEntityID { get { return h_entityid.Value; } set { h_entityid.Value = value; } }

        /// <summary>
        /// 下拉列表ui的宽度 比文本框宽10像素
        /// </summary>
        public int UIWidth { get; set; }

        /// <summary>
        /// 文本框搜索文本
        /// </summary>
        public string TextboxValue { get { return query.Text; } }
        #endregion
        #region 扩展js事件
        /// <summary>
        /// 搜索结果下拉框单击事件(js onclick事件)
        /// </summary>
        public string UlClickEvent { get; set; }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (UlClickEvent.IsEmpty())
                {
                    UlClickEvent = "javascript:void(0);";
                }
                c_FilePathDepth.Value = Method.RelativePathDepth;
                selectorlblmsg.Text = EmptyTip;

                if (MaxItemCount > 0)
                {
                    c_MaxItemCount.Value = MaxItemCount.ToString();
                }
                if (TextBoxWidth > 0)
                {
                    UIWidth = TextBoxWidth + 10;
                    query.Width = TextBoxWidth;
                }
                if (TextBoxWatermarkText.IsNotEmpty())
                {
                    query.Attributes.Add("placeholder", TextBoxWatermarkText);
                }
                if (DefaultValue.IsNotEmpty())
                {
                    query.Text = DefaultValue;
                }
                if (AutoHideWhenBlur)
                {
                    //this.Page.Form.Attributes.Add("onclick", "brandListLiHide()");
                    this.Page.Form.Attributes.Add("onclick", "h()");
                }
                if (ServiceFilePath.IsNotEmpty())
                {
                    c_FilePath.Value = ServiceFilePath;
                }
                if (CustomizeQuery.IsNotEmpty())
                {
                    if (!CustomizeQuery.StartsWith("&"))
                        c_customizeQuery.Value = "&" + CustomizeQuery;
                    else
                        c_customizeQuery.Value = CustomizeQuery;
                }
            }
        }
    }
}